package com.bfmet.servlet;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.font.TextAttribute;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.text.AttributedString;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;

import com.bfmet.common.base.ServletBase;
import com.bfmet.entity.Price;

@WebServlet("/chart/*")
public class ChartServlet extends ServletBase {

	@Override
	protected void doGet(ServletRequest request, ServletResponse response)
			throws ServletException, IOException {

		try {
			BufferedImage image = new BufferedImage(400, 170,
					BufferedImage.TYPE_INT_RGB);

			Graphics2D g = image.createGraphics();
			g.setColor(Color.white);
			g.fill3DRect(0, 0, 400, 170, true);

			g.setColor(Color.green);
			Line2D lin = new Line2D.Double(50, 40, 100, 40);
			g.draw(lin);

			g.setColor(Color.blue);
			String title = "BFMI @"
					+ new SimpleDateFormat("yyyy-MM-dd-ss").format(new Date());
			AttributedString as = new AttributedString(title);
			as.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
			as.addAttribute(TextAttribute.SIZE, 10);
			g.drawString(as.getIterator(), 130, 15);

			GeneralPath path = new GeneralPath();
			path.moveTo(30, 150);
			g.setStroke(new BasicStroke(1));
			g.draw(path);

			for (int k = 0; k < 4; k++) {
				float x = 5, y = 40 * k + 30;
				g.setColor(Color.black);
				g.drawString(String.valueOf(2500 - k * 100), x, y);
			}
			g.setColor(Color.lightGray);
			path.moveTo(40, 20);
			path.lineTo(40, 150);
			path.lineTo(380, 150);
			path.lineTo(380, 20);
			path.lineTo(40, 20);
			g.draw(path);
			GeneralPath changePath = new GeneralPath();
			changePath.moveTo(40, 150);
			g.setColor(Color.magenta);
			List<Price> list = this.getMockData();
			for (int i = 1; i <= list.size(); i++) {
				Price price = list.get(i - 1);
				int averagePrice = Integer.valueOf(price.getAverage());
				log("AveratePrice:" + averagePrice);
				float x = 60 * i + 40;
				float y = 150 - 130 * (averagePrice - 2200) / 300;
				log("y:" + y);
				changePath.lineTo(x, y);
				g.drawString(new SimpleDateFormat("MM-dd").format(price
						.getCreateTime()), x - 20, 165);
			}
			g.draw(changePath);

			response.setContentType("image/png");
			OutputStream os = response.getOutputStream();
			ImageIO.write(image, "png", os);
			os.close();
		} catch (Exception ex) {
			this.logException(ex);
		}
	}

	public List<Price> getMockData() {
		List<Price> list = new ArrayList<Price>();
		for (int i = 0; i < 5; i++) {
			Price price = new Price();
			price.setName("Cu");
			price.setAverage(String.valueOf(2200 + new Random().nextInt(300)));
			Calendar ca = Calendar.getInstance();
			ca.add(Calendar.DAY_OF_YEAR, -20);
			ca.add(Calendar.DAY_OF_YEAR, 5 * i);
			price.setCreateTime(ca.getTime());
			list.add(price);
		}
		return list;
	}
}
